package menu

import (
	"context"
	"encoding/json"
	"fast-boot/app/model"
	"github.com/jinzhu/copier"

	"fast-boot/app/api/admin/internal/svc"
	"fast-boot/app/api/admin/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 获取菜单
func NewGetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLogic {
	return &GetLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetLogic) Get(in *types.SysMenuGetReq) (*types.SysMenuInfo, error) {
	item := model.SysMenuModel{}
	l.svcCtx.GormClient.GormDb.First(&item, in.Id)
	var resp types.SysMenuInfo
	if err := copier.Copy(&resp, item); err != nil {
		return nil, err
	}
	tmp := make([]*types.ParamsItem, 0)
	_ = json.Unmarshal(item.Params, &tmp)
	resp.Params = tmp

	return &resp, nil
}
